package cn.ococci.dm.Main.View;

/**
 * Created by cyan on 16/1/27.
 */

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import cn.ococci.dm.Main.Tools.ScreenTool;
import cn.ococci_dm.R;


public class SwitchButton extends View implements View.OnTouchListener {

    private float nowX; // 当前按下的X和当前的X
    private long downTime;

    private boolean checkStatus = false; // 当前的状态

    private Paint paint;
    private ChangeStateListener changeListener;

    private float height;
    private float width;
    private float radius;
    private float leftX;
    private float rightX;

    private float centerY;
    private float dp;
    private int themeColor;
    private boolean touchAble;

    private int targetId = -1;

    private RectF bgRect;

    public SwitchButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initStyle();
        this.setOnTouchListener(this);
        setChecked(true);
    }

    private void initStyle() {
        dp = ScreenTool.dpToPx(1);
        width = ScreenTool.dpToPx(50);
        height = ScreenTool.dpToPx(25);

        themeColor = getResources().getColor(R.color.colorPrimary);

        bgRect = new RectF(dp, dp, width - dp, height - dp);
        radius = height / 2 - dp;

        leftX = radius + dp;
        rightX = width - dp - radius;

        centerY = height / 2;

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(ScreenTool.dpToPx(2));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(Color.LTGRAY);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawRoundRect(bgRect, radius, radius, paint);

        if (nowX > leftX) {
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(themeColor);
            RectF rightRect = new RectF( dp, dp, nowX - dp+radius,
                    height - dp);
            canvas.drawRoundRect(rightRect, radius, radius, paint);
        }
        paint.setColor(Color.LTGRAY);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(nowX, centerY, radius, paint);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(nowX, centerY, radius - dp, paint);

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (touchAble) {
            return false;
        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                nowX = event.getX();
                downTime = System.currentTimeMillis();
                break;
            case MotionEvent.ACTION_MOVE:
                nowX = event.getX();
                if (nowX < leftX) {
                    nowX = leftX;
                } else if (nowX > rightX) {
                    nowX = rightX;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                long upTime = System.currentTimeMillis();
                if (upTime - downTime < 100) {
                    changeStatus();
                } else {
                    float centerX = width / 2;
                    if (nowX < centerX) {
                        if (checkStatus)
                            this.onChangeStatus(false);
                        setChecked(false);
                    } else {
                        if (!checkStatus)
                            this.onChangeStatus(true);
                        setChecked(true);
                    }
                }
                break;
        }

        this.invalidate();
        return true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        this.setMeasuredDimension((int) width, (int) height);
    }

    private void onChangeStatus(boolean status) {
        if(changeListener != null){
            if (targetId > 0) {
                this.changeListener.onChangeState(targetId, status);
            } else {
                this.changeListener.onChangeState(status);
            }
        }

    }

    /**
     * 设置滑动开关的初始状态，供外部调用
     *
     * @param checked
     */
    public void setChecked(boolean checked) {
        if (!checked) {
            nowX = leftX;
        } else {
            nowX = rightX;
        }
        checkStatus = checked;

    }

    private void changeStatus() {
        this.onChangeStatus(!checkStatus);
        this.setChecked(!checkStatus);
    }

    public interface ChangeStateListener {
        void onChangeState(boolean checkState);

        void onChangeState(int id, boolean checkState);
    }

    public ChangeStateListener getChangeListener() {
        return changeListener;
    }

    public void setChangeListener(ChangeStateListener changeListener) {
        this.changeListener = changeListener;
    }

    public void setTargetId(int targetId) {
        this.targetId = targetId;
    }

    public void setTouchDisable(boolean touchAble) {
        this.touchAble = touchAble;
    }

    public boolean getTouchDiable() {
        return touchAble;
    }
}

